using System;
using System.Collections.Generic;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Wintellect.Sterling;
using Wintellect.Sterling.IsolatedStorageUpgrade;
using Wintellect.Sterling.IsolatedStorage;

namespace SharpDropBoxClient.db
{
    public class EngineManager
    {
        #region Singleton pattern From Jon Skeet - http://www.yoda.arachsys.com/csharp/singleton.html

        static readonly EngineManager instance = new EngineManager();

        // Explicit static constructor to tell C# compiler
        // not to mark type as beforefieldinit
        static EngineManager()
        {
        }

        EngineManager()
        {
            _ActivateEngine();
        }
        private void _ActivateEngine()
        {
            Upgrade.DoUpgrade(_EngineReady);
        }

        private void _EngineReady()
        {
            Engine = new SterlingEngine();

            // Custom Serializers/loggers go here

            // End Custom Serializers/loggers

            Engine.Activate();

            // Register DBs
            DropBoxCacheDb.Instance = Engine.SterlingDatabase.RegisterDatabase<DropBoxCacheDb>(new IsolatedStorageDriver());
            // End Register DBs
        }
        public void Finalize()
        {
            Engine.Dispose();
        }

        public static EngineManager Instance
        {
            get
            {
                return instance;
            }
        }

        #endregion Singleton pattern From Jon Skeet - http://www.yoda.arachsys.com/csharp/singleton.html

        public SterlingEngine Engine { get; private set; }
    }
}